package com.education.api.controller;

import com.alibaba.fastjson.JSONObject;
import com.education.common.annotation.FormLimit;
import com.education.common.utils.Result;
import com.education.common.utils.ResultCode;
import com.education.model.request.StudentQuestionRequest;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

@RestController
@RequestMapping("/dataease")
public class DataEaseController {

    @PostMapping("/open")
    public Result open(@RequestBody JSONObject jsonObject) {
        try {
            ProcessBuilder pb = new ProcessBuilder("D:\\develop\\DataEase-win32-x64\\DataEase.exe");

            // 重定向错误流以便捕获输出
            pb.redirectErrorStream(true);

            // 设置工作目录到应用安装目录
            pb.directory(new File("D:\\develop\\DataEase-win32-x64"));

            Process process = pb.start();

            // 读取输出流
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println("APP OUTPUT: " + line);
            }

            int exitCode = process.waitFor();
            System.out.println("Exit Code: " + exitCode);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return Result.success("操作成功");
    }
}
